#!/bin/bash

# 临时文件存储统计结果
tmpfile=$(mktemp gitstats.tmp)
tmpBinFile=$(mktemp gitbin.txt)

# 获取当天所有提交（排除合并提交）
# 当天 git log --since="midnight" --no-merges --format="%H %aN <%aE>" 
# 一段时间 git log --since=2025-04-01 --until=2025-08-19 --no-merges --format="%H %aN <%aE>"
# 昨天 git log --since="$(date -v-1d +"%Y-%m-%dT00:00:00")" --until="$(date +"%Y-%m-%dT00:00:00")" --no-merges --format="%H %aN <%aE>"
git log --since=2025-04-01 --until=2025-08-19 --no-merges --format="%H %aN <%aE>" | while read -r commit author; do
    author=$(echo "$author" | sed 's/[[:space:]]*$//')  # 清理尾部空格
    
    git show --numstat --format="" "$commit" 2>/dev/null | while IFS=$'\t' read -r add del file; do
        file=$(echo "$file" | xargs)

        # 处理二进制文件（包含删除情况）
        if [[ "$add" == "-" && "$del" == "-" ]]; then
            # 获取当前提交文件体积
            current_size=$(git show "$commit":"$file" 2>/dev/null | wc -c 2>/dev/null || echo 0)
            current_size=${current_size// /}

            # 获取父提交文件体积
            if git rev-parse --quiet --verify "$commit"^ >/dev/null; then
                parent_size=$(git show "$commit"^:"$file" 2>/dev/null | wc -c 2>/dev/null || echo 0)
            else
                parent_size=0
            fi
            parent_size=${parent_size// /}

            # 记录体积变化（含正负）
            echo "$author binary $((current_size - parent_size))" >> "$tmpfile"
            # 计算差值（字节）
            diff=$((current_size - parent_size))
            # 转换为 KB（保留两位小数）
            diff_kb=$(awk -v d="$diff" 'BEGIN { printf "%.2f", d / 1024 }')
            # 输出到文件
            echo "$author $file ${diff_kb} KB" >> "$tmpBinFile"
            #echo "$author $file $((current_size - parent_size))" >> "$tmpBinFile"

        # 处理文本文件（记录新增和删除）
        elif [[ "$add" =~ ^[0-9]+$ ]]; then
            echo "$author text_add $add" >> "$tmpfile"
            echo "$author text_del $del" >> "$tmpfile"
        fi
    done
done

echo "---------------------------统计---------------------------"
printf "%-25s | %-14s | %s\n" "作者" "代码行净增减" "体积变化(KB)"
echo "-----------------------------------------------------------"

awk '
BEGIN {
    OFS = " | "
    PROCINFO["sorted_in"] = "@ind_str_asc"
}
{
    # 解析作者信息
    author = $1
    for (i=2; i<NF-1; i++) author = author " " $i
    
    # 分类统计
    if ($(NF-1) == "text_add") {
        add[author] += $NF
    } else if ($(NF-1) == "text_del") {
        del[author] += $NF
    } else if ($(NF-1) == "binary") {
        binary[author] += $NF
    }
}
END {
    # 合并所有作者
    for (a in add) all_authors[a]++
    for (a in del) all_authors[a]++
    for (a in binary) all_authors[a]++

    # 输出结果
    for (a in all_authors) {
        # 计算代码行净变化
        net_lines = (add[a] ? add[a] : 0) - (del[a] ? del[a] : 0)
        
        # 计算体积变化（KB四舍五入）
        kb_change = sprintf("%.0f", (binary[a] ? binary[a] : 0)/1024)
        
        # 格式化负数显示
        lines_str = net_lines
        #if (net_lines < 0) lines_str = "\033[31m" net_lines "\033[0m"
        
        kb_str = kb_change
        #if (kb_change < 0) kb_str = "\033[31m" kb_change "\033[0m"
        
        printf "%-25s | %-14s | %s\n", a, lines_str, kb_str
    }
}' "$tmpfile" | column -t -s "|"

rm -f "$tmpfile"
# 显示结果（带表头和格式化）
echo "---------------------------详细---------------------------"
printf "%-25s | %-14s | %s\n" "作者" "文件" "体积(KB)"
echo "-----------------------------------------------------------"
cat "$tmpBinFile" | column -t -s " "
rm -f "$tmpBinFile"
